package com.lvt.example.thinking.in.spring.event;

import java.util.EventListener;
import java.util.EventObject;
import java.util.Observable;
import java.util.Observer;

/**
 * desc：{@link java.util.Observer} 示例
 *
 * @author LT
 * @date 2022/9/19
 */
public class ObserverDemo {

    public static void main(String[] args) {
        Observable observable = new EventObservable();
        observable.addObserver(new EventObserver());
        observable.notifyObservers("hello world");
    }


    static class EventObservable extends Observable {
        @Override
        protected synchronized void setChanged() {
            super.setChanged();
        }

        @Override
        public void notifyObservers(Object arg) {
            setChanged();
            super.notifyObservers(new EventObject(arg));
            clearChanged();
        }
    }


    static class EventObserver implements Observer, EventListener {

        @Override
        public void update(Observable o, Object message) {
            EventObject eventObject = (EventObject) message;
            System.out.println("收到消息"+eventObject);
            System.out.println("收到消息"+eventObject.getSource());
        }
    }
}
